package com.wash.shoes.controller.mobile.test;


import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;


import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import com.wechat.pay.java.service.refund.RefundService;
import com.wechat.pay.java.service.refund.model.AmountReq;
import com.wechat.pay.java.service.refund.model.CreateRequest;
import com.wechat.pay.java.service.refund.model.Refund;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;


@Service
public class WechatPayService {
    public static JsapiServiceExtension jsapiServiceExtension;
    public static RefundService refundService;

    private final WxPayConfigNew wxPayConfig;

    public static final String CNY = "CNY";

    @Autowired
    public WechatPayService(WxPayConfigNew wxPayConfig) throws Exception {
        this.wxPayConfig = wxPayConfig;
        this.init();
    }

    @PostConstruct
    public void init() throws Exception {
        String privateKey = WxPayUtilNew.loadKeyByResource("apiclient_key.pem");
        // 初始化商户配置
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(wxPayConfig.getMCHID())
                        .privateKey(privateKey)
                        .merchantSerialNumber(wxPayConfig.getSERIAL_NO())
                        .apiV3Key(wxPayConfig.getApiV3key())
                        .build();
        // 初始化服务
        jsapiServiceExtension =
                new JsapiServiceExtension.Builder()
                        .config(config)
                        .signType("RSA") // 不填默认为RSA
                        .build();
        refundService = new RefundService.Builder().config(config).build();


    }

    /**
     * JSAPI支付下单，并返回JSAPI调起支付数据
     *
     * @param details    订单描述
     * @param outTradeNo id
     * @param money      金额
     * @param openId     用户openid
     * @return PrepayWithRequestPaymentResponse 支付信息
     */
    public PrepayWithRequestPaymentResponse prepayWithRequestPayment(String details, String outTradeNo,
                                                                     BigDecimal money, String openId) {
        PrepayRequest request = new PrepayRequest();

        Amount amount = new Amount();
        amount.setTotal(decimalToInt(money));
        amount.setCurrency(CNY);

        request.setNotifyUrl(wxPayConfig.getPayNotifyUrl());
        request.setAmount(amount);

        request.setAppid(wxPayConfig.getAppid());
        request.setMchid(wxPayConfig.getMCHID());
        request.setOutTradeNo(outTradeNo);
        request.setDescription(details);

        Payer payer = new Payer();
        payer.setOpenid(openId);
        request.setPayer(payer);
        // 调用接口
        return jsapiServiceExtension.prepayWithRequestPayment(request);
    }

    /**
     * 退款申请
     **/
    public Refund createRefund(String outTradeNo, BigDecimal finalAmount) {
        CreateRequest request = new CreateRequest();
        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
        request.setOutTradeNo(outTradeNo);
        request.setOutRefundNo("REFUND_" + outTradeNo);

        AmountReq amount = new AmountReq();
        amount.setTotal(decimalToLong(finalAmount));
        amount.setRefund(decimalToLong(finalAmount));
        amount.setCurrency(CNY);

        request.setAmount(amount);
//        request.setNotifyUrl(wxPayConfig.getRefundNotifyUrl());
        // 调用接口
        return refundService.create(request);
    }

    private static int decimalToInt(BigDecimal money) {
        return money.multiply(BigDecimal.valueOf(100)).intValue();
    }

    private static long decimalToLong(BigDecimal money) {
        return money.multiply(BigDecimal.valueOf(100)).longValue();
    }
}

